package modules::stat_preprocessing;
use modules::utilities;
use Data::Dumper;
sub new {
  my ($class,$config) = @_;
  my $self = {
               _config => $config,
             };
  bless $self,$class;

  return $self;
}

sub read_gtf {
  my $self = shift;
  my $path = $self->{'_config'}{'GTF_FILES'};
  open(my $fh,'<',$path);
  my $gtf;
  while(<$fh>) {
    print $_;
  }
  return;
}

sub run {
  my $self = shift;
  my $samples            = $self->{'_samples'     };
  my $path               = $self->{'_path'        };
  my $genome             = $self->{'_genome'      };
  my $fastq_dir          = $self->{'_fastq_dir'   };
  my $gtf_file           = $self->{'_gtf_file'    };
  my $tophat_out         = $self->{'_tophat_out'  };
  my $tophat_conf        = $self->{'_tophat_conf' };
  my $insert_size        = $self->{'_r'};
  my $i=1;
  foreach my $sample(@$samples) { 
    my $r       = $insert_size->[$i-1];
    my    $cmd  =  qq(bsub -J tophat_$i -n 4 -R "span[ptile=4] rusage[mem=5000]" )
                .  qq(/usr/users/ga002/corpasm/workarea/bin/tophat -p 8 -r $r -G $gtf_file -o $tophat_out/$sample->[0] );
    #map { $cmd  .= qq($_ $tophat_conf->{$_} )   }  keys %$tophat_conf; # map each option
          $cmd  .= qq($genome );
    map { $cmd  .= qq($fastq_dir/$_.txt  )    }  @$sample; # map each replica to the command
    print "\n\nRUNNING COMMAND:\n$cmd\n\n";
    system($cmd);
    $i++;
  }

  my $wait = 1;
  while ($wait == 1) {
    my  @info = qx(bjobs);
    print scalar localtime() . "\n";
    if(my @tophat_jobs = grep(/tophat_/, @info) ) { map{print $_}@tophat_jobs; sleep 10 }
    else {$wait = 0}  
  }

  return;
}
1;